package com.stenio.fancat.service.wx;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stenio.fancat.common.FanCatException;
import com.stenio.fancat.domain.baby.BabyAccount;
import com.stenio.fancat.domain.shop.FcEtOrder;
import com.stenio.fancat.domain.shop.FcGift;
import com.stenio.fancat.domain.user.UserRelationship;
import com.stenio.fancat.domain.user.UserRelationshipFomId;
import com.stenio.fancat.form.OrderForm;
import com.stenio.fancat.mapper.baby.BabyAccountMapper;
import com.stenio.fancat.mapper.shop.FcGiftMapper;
import com.stenio.fancat.mapper.user.UserRelationshipFomIdMapper;
import com.stenio.fancat.mapper.user.UserRelationshipMapper;
import com.stenio.fancat.pay.Notice;
import com.stenio.fancat.pay.OrderResult;
import com.stenio.fancat.pay.Pay;
import com.stenio.fancat.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 支付模块服务实现类
 */
@Service
public class PayService {

	private static final Logger logger = LoggerFactory.getLogger(PayService.class);

	@Autowired
	private Pay pay;

	@Autowired
	private FcEtOrderService fcEtOrderService;

	@Autowired
	private WxPushServiceQcl wxPushServiceQcl;

	@Autowired
	private UserRelationshipMapper userRelationshipMapper;

	@Autowired
	private UserRelationshipFomIdMapper userRelationshipFomIdMapper;

	@Autowired
	private BabyAccountMapper babyAccountMapper;

	@Autowired
	private FcGiftMapper fcGiftMapper;

	@Value("${templatePic_exceptional}")
	private String templatePicExceptional;


	//调用微信支付接口
	public OrderResult placeOrder(OrderForm form) throws FanCatException {
		OrderResult result = pay.placeOrder(form);
		return result;
	}


	public int processNotice(Notice notice) throws FanCatException {
		if (null == notice)
			throw new RuntimeException("微信通知XML解析结果为空");
		logger.info("WX notice==>{}", notice.toString());
		Integer i = pay.processNotice(notice);
		if(i>0) {
			processOrder(notice);
		}
		return i;
	}


	public void processOrder(Notice notice) {
		// 通过订单编号获取订单详
		String orderNo = notice.getOut_trade_no();
		// 更新订单状态为已支付
		FcEtOrder entity = new FcEtOrder();
		entity.setStatus(1);
		fcEtOrderService.update(entity, new EntityWrapper<FcEtOrder>().eq(FcEtOrder.OUT_TRADE_NO, orderNo));
		FcEtOrder fcEtOrder = fcEtOrderService.selectOne(new EntityWrapper<FcEtOrder>().eq(FcEtOrder.OUT_TRADE_NO, orderNo));
		UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(fcEtOrder.getUserId());
		UserRelationship etUserRelationship = userRelationshipMapper.selectUserInfoById(fcEtOrder.getEtUserId());
		UserRelationshipFomId userRelationshipFomId = userRelationshipFomIdMapper.selectFormIdByUid(fcEtOrder.getUserId());
		BabyAccount babyAccount = babyAccountMapper.selectByBid(fcEtOrder.getBabyId());
		FcGift fcGift = fcGiftMapper.selectById(fcEtOrder.getGiftId());
		double money = fcEtOrder.getFee() * 0.01;
		String[] keys = new String[5];
		keys[0] = etUserRelationship.getName()+"给您的宝宝"+babyAccount.getNickName()+"一个"+fcGift.getName();
		keys[1] = money+"元";
		keys[2] = DateUtil.format(fcEtOrder.getCreateTime(),"yyyy-MM-dd HH:mm");
		wxPushServiceQcl.pushOneUser(userRelationship.getOpenId(),
				userRelationshipFomId.getFormId(),
				templatePicExceptional,
				keys,
				fcEtOrder.getPage());
		userRelationshipFomIdMapper.modifyFormIdByUid(userRelationship.getUserId(),userRelationshipFomId.getFormId());
	}

}
